package cn.com.pansky.action.myaccount;

import java.sql.Timestamp;

import com.alibaba.fastjson.JSONObject;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import cn.com.pansky.service.CommonService;
import cn.com.pansky.util.MD5Util;

public class MessCodeAction extends CommonAction {
	CommonService cs = new CommonService();
	/**
	 * 判断账户是否存在并发送验证码
	 * @throws Exception 
	 */
	public void isUser() throws Exception{
		
		String AA032 = getParam("AA032");
		String findUser = urlConfig.getVar("findUser");
		Model model = SqlHelper.queryOne(findUser);
		if (model != null) {
			JSONObject obj = cs.aliyunSms(AA032);
			if (!"0".equals(obj.getString("code"))) {
				JSONObject json = new JSONObject();
				json.put("isPass", false);
				json.put("info", obj.getString("msg"));
				context.getDatas().put("result", json);
				return;
			}
			String code = obj.getString("content");
			context.getServletSession().setAttribute("AA032", AA032);
			context.getServletSession().setAttribute("AF016", code);
			SqlHelper.execute("insert into CA20 values(${~UUID}, ?, ?, ?, ?)", AA032,
					new java.sql.Timestamp(System.currentTimeMillis()),code,new Timestamp(System.currentTimeMillis()).getTime());
			context.getServletSession().setAttribute("AA032", AA032);
			context.getServletSession().setAttribute("AF016", code);
			
			datas.put("isPass", true);
			datas.put("info", "验证码已发送");
		}else{
			datas.put("isPass", false);
			datas.put("info", "该手机号未注册");
		}
		context.getDatas().put("result", datas);
	}
	
	/**
	 * 校验验证码
	 * 
	 * @throws Exception
	 */
	public void validateCode() throws Exception {
		if (context.getSessionVal("AA032")!=null && context.getSessionVal("AF016")!=null) {
			String AA002 = getParam("AA002");
			Model m = SqlHelper.queryOne("select * from (select * from CA20 where AA032 = ? and AF016 = ? order by AF014 desc) where rownum = 1", 
					getParam("AA032"),getParam("AF016"));
			//当前时间
			long nowTime = new Timestamp(System.currentTimeMillis()).getTime();
			if (m != null) {
				//验证码发送时间
				long AF017 = m.getLong("AF017");
				double time = (nowTime - AF017)/1000/60;
				if (time>5) {
					datas.put("isPass", false);
					datas.put("info", "验证码超时");
				}else{
					updPass(AA002);
				}
			} else {
				datas.put("isPass", false);
				datas.put("info", "验证码输入错误");
			}
		}else{
			datas.put("isPass", false);
			datas.put("info", "验证码已过期，请重新获取");
		}
		context.getDatas().put("result", datas);
	}
	
	/**
	 * 修改密码
	 */
	public void updPass(String AA002){
		//获取用户ID
		String findUser = urlConfig.getVar("findUser");
		String AA001 = SqlHelper.queryOne(findUser).getString("AA001");
		System.out.println(getParam("AA032"));
		if (getParam("AA032").equals(context.getSessionVal("AA032")) && getParam("AF016").equals(context.getSessionVal("AF016"))) {
			//修改新密码
			String updatePass = urlConfig.getVar("updatePass");
			AA002 = MD5Util.string2MD5(AA002);
			SqlHelper.execute(updatePass, AA002, AA001);
			
			datas.put("isPass", true);
			datas.put("info", "修改成功");
			context.getServletSession().removeAttribute("AA032");
			context.getServletSession().removeAttribute("AF016");
		}else{
			datas.put("isPass", false);
			datas.put("info", "请确认是已获取验证码的原手机号");
		}
		context.getDatas().put("result", datas);
	}
}
